import 'package:hinge/common/models/user_model.dart';

class TopicCommentModel {
  final int id;
  final int topicId;
  final int userId;
  final UserModel? user; // 关联的用户
  final DateTime timestamp;
  final String content;

  TopicCommentModel({
    required this.id,
    required this.topicId,
    required this.userId,
    this.user,
    required this.timestamp,
    required this.content,
  });

  // 从JSON构造
  factory TopicCommentModel.fromJson(Map<String, dynamic> json) {
    return TopicCommentModel(
      id: json['id'],
      topicId: json['topicId'],
      userId: json['userId'],
      user: json['user'] != null ? UserModel.fromJson(json['user']) : null,
      timestamp: DateTime.parse(json['timestamp']),
      content: json['content'],
    );
  }

  // 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'topicId': topicId,
      'userId': userId,
      'user': user?.toJson(),
      'timestamp': timestamp.toIso8601String(),
      'content': content,
    };
  }

  // 复制并修改评论对象
  TopicCommentModel copyWith({
    int? id,
    int? topicId,
    int? userId,
    UserModel? user,
    DateTime? timestamp,
    String? content,
  }) {
    return TopicCommentModel(
      id: id ?? this.id,
      topicId: topicId ?? this.topicId,
      userId: userId ?? this.userId,
      user: user ?? this.user,
      timestamp: timestamp ?? this.timestamp,
      content: content ?? this.content,
    );
  }
}
